package com.sunmnet.j2ee.service.powerdb.designer.impl;

import com.sunmnet.j2ee.core.powerdb.designer.Commons;
import com.sunmnet.j2ee.dao.BaseDao;
import com.sunmnet.j2ee.entity.powerdb.designer.Entity;
import com.sunmnet.j2ee.service.impl.BaseServiceImpl;
import com.sunmnet.j2ee.service.powerdb.designer.EntityService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("/powerdb/designer/entityService")
public class EntityServiceImpl extends BaseServiceImpl<Entity,Long> implements EntityService {

    private static final Logger LOG = LogManager.getLogger(EntityServiceImpl.class.getName());

    private static final String FIND_SQL_FOR_SAVE = "from Entity t where (upper(t.name)=upper(:name) OR upper(t.nameCn)=upper(:nameCn)) AND t.moduleId = :moduleId AND t.versionId = " + Commons.DEF_VERSION_ID;
    private static final String FIND_SQL_FOR_UPDATE = "from Entity t where (upper(t.name)=upper(:name) OR upper(t.nameCn)=upper(:nameCn)) AND t.moduleId = :moduleId and t.id!=:id AND t.versionId = " + Commons.DEF_VERSION_ID;


    @Override
    @Resource(name = "/powerdb/designer/entityDao")
    public void setDao(BaseDao<Entity, Long> dao) {
        super.dao = dao;
    }

    @Override
    public int checkRepeat(Entity entity) {

        Map params = new HashMap();
        params.put("name", entity.getName());
        params.put("nameCn", entity.getNameCn());
        params.put("moduleId", entity.getModuleId());

        String sql = FIND_SQL_FOR_SAVE;
        if (entity.getId() != null) {
            sql = FIND_SQL_FOR_UPDATE;
            params.put("id", entity.getId());
        }

        List list = dao.find(sql, params);
        if (list != null && list.size() > 0) {
            LOG.debug("实体" + entity.getName() + "(" + entity.getNameCn() + ")已经存在");
            return 1;
        }
        return 0;
    }

    @Override
    public List<Entity> getEntitys(Long moduleId, Long versionId) {
        String hql = "FROM Entity WHERE moduleId = " + moduleId + " AND versionId = " + versionId;
        return dao.findByHql(hql);
    }

    @Override
    public Map<Long, Long> copyEntitys(List<Entity> list, Long versionId, Long newModuleId) {
        Map<Long, Long> copyedEntitys = new HashMap<>();
        Entity tmp = null;
        for (Entity en : list) {
            tmp = clone(en, versionId, newModuleId);
            dao.save(tmp);
            copyedEntitys.put(en.getId(), tmp.getId());
        }
        LOG.debug("copyEntityRelates: {},{},{}, result: {} ", list, versionId, newModuleId, copyedEntitys);
        return copyedEntitys;
    }

    public Entity clone(Entity en, Long versionId, Long newModuleId) {
        Entity entity = new Entity();
        entity.setVersionId(versionId);
        entity.setModuleId(newModuleId);
        entity.setCreateTime(en.getCreateTime());
        entity.setModifyTime(en.getModifyTime());
        entity.setCreateAccountId(en.getCreateAccountId());
        entity.setModifyAccountId(en.getModifyAccountId());
        entity.setNameCn(en.getNameCn());
        entity.setName(en.getName());
        entity.setType(en.getType());
        entity.setEngine(en.getEngine());
        entity.setRemark(en.getRemark());
        entity.setScript(en.getScript());
        entity.setProjectId(en.getProjectId());
        entity.setxPosition(en.getxPosition());
        entity.setyPosition(en.getyPosition());
        entity.setDatabaseId(en.getDatabaseId());
        return entity;
    }
}
